//-----------------------------------------------------------------------
// <copyright file="a_Countries_SDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Countries_SDAOBase
    {
        #region Common methods
        public virtual a_Countries_S Createa_Countries_SFromReader(IDataReader reader)
        {
            a_Countries_S item = new a_Countries_S();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryISO2"))) item.CountryISO2 = (string)reader["CountryISO2"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryISO3"))) item.CountryISO3 = (string)reader["CountryISO3"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryName"))) item.CountryName = (string)reader["CountryName"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Countries_S methods
            
        public virtual void Createa_Countries_S(a_Countries_S a_Countries_S)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_S_ins");
                
                database.AddInParameter(dbCommand, "@CountryISO2", DbType.AnsiString, a_Countries_S.CountryISO2);
                database.AddInParameter(dbCommand, "@CountryISO3", DbType.AnsiString, a_Countries_S.CountryISO3);
                database.AddInParameter(dbCommand, "@CountryName", DbType.AnsiString, a_Countries_S.CountryName);
                database.AddOutParameter(dbCommand, "@CountryID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Countries_S.CountryID = (int)database.GetParameterValue(dbCommand, "@CountryID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Countries_S methods
        
        public virtual void Updatea_Countries_S(a_Countries_S a_Countries_S)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_S_upd");            
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Countries_S.CountryID);
                database.AddInParameter(dbCommand, "@CountryISO2", DbType.AnsiString, a_Countries_S.CountryISO2);
                database.AddInParameter(dbCommand, "@CountryISO3", DbType.AnsiString, a_Countries_S.CountryISO3);
                database.AddInParameter(dbCommand, "@CountryName", DbType.AnsiString, a_Countries_S.CountryName);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Countries_S methods
        public virtual void Deletea_Countries_S(int countryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_S_del");
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, countryID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Countries_S methods
        
        public virtual a_Countries_S Geta_Countries_S(int countryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_S_sel");
                
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, countryID);
                
                a_Countries_S a_Countries_S =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Countries_S = Createa_Countries_SFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Countries_S;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Countries_SList methods
        public virtual a_Countries_SCollection Geta_Countries_SList(a_Countries_SColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Countries_S_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Countries_SCollection a_Countries_SCollection = new a_Countries_SCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Countries_S a_Countries_S = Createa_Countries_SFromReader(reader);
                        a_Countries_SCollection.Add(a_Countries_S);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Countries_SCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Countries_SCollection Geta_Countries_SList(a_Countries_SColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Countries_SList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}